<?php
/* @var $this ProductBrandController */
/* @var $model ProductBrand */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'product-brand-form',
	'enableAjaxValidation'=>false,
        'enableClientValidation'=>true
)); ?>

        <?php echo $form->hiddenField($model, 'id', array('id'=>'brandId')); ?>
	<div class="row">
		<?php echo $form->labelEx($model,'name'); ?>
		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>200)); ?>
		<?php echo $form->error($model,'name'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::ajaxSubmitButton(
                            Yii::t('main', 'Create New Brand'), 
                            Yii::app()->createUrl('admin/productBrand/save'),
                            array(
                                'dataType'=>'json',
                                'success'=>'onSaveResponed'
                            ),
                            array('id'=>'btnsubmit')
                ); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">
    function setFormData(data){
        if(data.hasOwnProperty('id')){
            $('#brandId').val(data.id);
        }
        
        if(data.hasOwnProperty('name')){
            $('#ProductBrand_name').val(data.name);
        }
    }
    
    function clearFormData(){
        $('#brandId').val('');
        $('#ProductBrand_name').val('');
        $('#btnsubmit').val('<?php echo Yii::t('main', 'Create New Brand') ?>');        
    }
    
    function beginUpdate(id){
        $.ajax({
            'url':'<?php echo Yii::app()->createUrl('admin/productBrand/getBrandData') ?>/id/'+id,
            'dataType':'json',
            'success':function(data){
                if(data.success===1){
                    $('#btnsubmit').val('<?php echo Yii::t('main', 'Update Brand') ?>');
                    setFormData(data.data);
                }else{
                    alert('Error get data');
                }
            }
        });
    }
    
    function onSaveResponed(data){
        if(data.success===1){
            clearFormData();
            
            //call function in admin.php page
            reloadGrid();
        }
    }
</script>